{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d955d75d-4970-48fe-983e-a2a850cecfc5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import requests\n",
    "from dotenv import load_dotenv\n",
    "from bs4 import BeautifulSoup\n",
    "from IPython.display import Markdown, display\n",
    "from openai import OpenAI\n",
    "\n",
    "import PyPDF2\n",
    "from selenium import webdriver\n",
    "from selenium.webdriver.chrome.options import Options\n",
    "from selenium.webdriver.chrome.service import Service\n",
    "from webdriver_manager.chrome import ChromeDriverManager\n",
    "from bs4 import BeautifulSoup\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6e1e5dd3-f91a-466b-8fd4-2dbf4eedf101",
   "metadata": {},
   "outputs": [],
   "source": [
    "load_dotenv(override = True)\n",
    "api_key = os.getenv(\"OPENAI_API_KEY\")\n",
    "\n",
    "if not api_key:\n",
    "    print(\"No API key\")\n",
    "elif not api_key.startswith(\"sk-proj-\"):\n",
    "    print(\"API key doesn't look correct, check it\")\n",
    "elif api_key.strip() != api_key:\n",
    "    print(\"It looks like API key has an extra space - check it\")\n",
    "else:\n",
    "    print(\"API key looks good, moving on!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "67a6e583-1ef7-4b77-8886-c0e8c619933c",
   "metadata": {},
   "outputs": [],
   "source": [
    "openai = OpenAI()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "34a07806-dd68-4a86-8b6e-e1b2aaf0daa1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# path to the CV\n",
    "path = \"/Users/yanasklar/Documents/For applying/CV/СV_YanaSklyar_c.pdf\"\n",
    "headers = {\n",
    "    \"User-Agent\": \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36\"\n",
    "}\n",
    "\n",
    "class Vacancy:\n",
    "    def __init__(self, url, instructions = \"\"):\n",
    "        self.url = url\n",
    "        \n",
    "        # configure Chrome settings\n",
    "        options = Options()\n",
    "        # options.add_argument(\"--headless\")  \n",
    "        \"\"\"\n",
    "        Headless mode runs the browser in the background (invisible).\n",
    "        However, some websites (like openai.com) block headless browsers.\n",
    "        So if this line is active, the page may not load correctly and you may not get the full content.\n",
    "        \"\"\"\n",
    "        options.add_argument(\"--disable-gpu\")\n",
    "        options.add_argument(\"--no-sandbox\")\n",
    "        options.add_argument(\"--window-size=1920x1080\")\n",
    "\n",
    "        # use webdriver-manager to manage ChromeDriver\n",
    "        service = Service(ChromeDriverManager().install())\n",
    "        driver = webdriver.Chrome(service=service, options=options)\n",
    "        driver.get(url)\n",
    "        time.sleep(3) # let the page load\n",
    "\n",
    "        # take the source of the page\n",
    "        page_source = driver.page_source\n",
    "        driver.quit()\n",
    "\n",
    "        # analyse with BeautifulSoup\n",
    "        soup = BeautifulSoup(page_source, 'html.parser')\n",
    "\n",
    "        self.title = soup.title.string if soup.title else \"No title found\"\n",
    "        for irrelevant in soup.body([\"img\", \"script\", \"style\", \"input\"]):\n",
    "            irrelevant.decompose()\n",
    "        self.text = soup.body.get_text(separator='\\n', strip=True)\n",
    "\n",
    "        # read CV\n",
    "        with open(path, 'rb') as f:\n",
    "            reader = PyPDF2.PdfReader(f)\n",
    "            cv_text = \"\"\n",
    "            for page in reader.pages:\n",
    "                text = page.extract_text()\n",
    "                if text:\n",
    "                    cv_text += text + \"\\n\"\n",
    "        self.cv_text = cv_text\n",
    "\n",
    "        # summarise and print the description of the job\n",
    "        message = f\"\"\"Here is the content of a webpage: {self.text}.\n",
    "        Find job description on that page,\n",
    "        summarise it, include the list requirements and other important details.\n",
    "        \"\"\"\n",
    "        messages = [{\"role\":\"user\", \"content\":message}]\n",
    "        response = openai.chat.completions.create(model='gpt-4o-mini', messages = messages)\n",
    "        print(\"The job description: \", response.choices[0].message.content)\n",
    "\n",
    "        # create prompts\n",
    "        self.system_prompt = \"\"\"You are a career assistant specializing in writing cover letter.\n",
    "        Your tasks:\n",
    "        1. Read the candidate's CV (provided as text).\n",
    "        2. Read the job description (provided from a webpage).\n",
    "        3. Write a concise and compelling cover letter, that:\n",
    "        - Hightlights the most relevant experience and  skills from the CV,\n",
    "        - Aligns directly wit the requirements in the job description,\n",
    "        - Adapts to cultural and professional norms in Israel.\n",
    "        The letter should be no longer than half a page, persuasive and tailored to make the applicant stand out.\n",
    "        \"\"\"\n",
    "\n",
    "        user_prompt = f\"\"\"\n",
    "            Here is my CV:\n",
    "            {self.cv_text}\n",
    "            \n",
    "            The job vacancy is from the website {self.title}.\n",
    "            Here is the decription of the vacancy:\n",
    "            {self.text}\n",
    "            Please write a cover letter that connects my background to this vacancy.\n",
    "            Make it persuasive and suitable for Israeli job market.\n",
    "            \"\"\"\n",
    "            \n",
    "        if instructions:\n",
    "            user_prompt += f\"Additional instructions: {instructions}\"\n",
    "        self.user_prompt = user_prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9160b9f5-177b-4477-8e54-3a212f275a22",
   "metadata": {},
   "outputs": [],
   "source": [
    "def cover_letter(url, instructions = \"\"):\n",
    "    vacancy = Vacancy(url, instructions)\n",
    "    messages = [\n",
    "        {\"role\":\"system\", \"content\":vacancy.system_prompt},\n",
    "        {\"role\":\"user\", \"content\":vacancy.user_prompt}\n",
    "    ]\n",
    "    response = openai.chat.completions.create(model='gpt-4o-mini', messages=messages)\n",
    "    if not response:\n",
    "        print(\"smt went wrong\")\n",
    "    print(response.choices[0].message.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1de4b55c-a8da-445f-9865-c7a8bafdbc3c",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = \"https://www.linkedin.com/jobs/view/4285898438/?alternateChannel=search&eBP=CwEAAAGY3R5LOabDLOVTy6xvBcSlWyAkIXQz8IRkSM3rgsqTPtvcEvUSnq980O7oLV2Hh_ldTpc2cBBmRq1IRnLtp7TzEcUvndFEXeCuviA5yo7oFYfW7KoEp4SPNzmf3D9LtnSgk9Iudy3skk6n3hVOtyDpx8Zm0AiTWPvdwCaZ_w5Xu8lAG797NRNDco71ynm99LmCOC9Go7DdDQ2eLewamc4SOsA4xWcXy0GmZVy3kBF1AprK3ylAYR2wrm5-hp4lRpbbfUxXjkEOG6H_GbPpKtN-N8mYnMd9w_cej5qQmTFX86gqSi6HuXFtK0h46TbOS5r-YQksVd1Yb4kYZnDznWXPLbxp04xVJSPzsHoa05wQdOfZ2UUSoMTJmic3n3qfV2u9Bp8n4sLYtINpzKdvm4eADGGkN-nR3O2oPeas9XjGbBwNdjXHAcX_PJoRwlFdQ1gVkYQEF1T7qAfXUJoUt-fv4oLxGnIgV6yJuMgw&refId=9NA7Bvt%2FhCqDkFNRGu1dPA%3D%3D&trackingId=W11hvpcIjHA%2FjU%2FFZ%2B1uAA%3D%3D\"\n",
    "b = \"The style of the cover letter should informal, as if i talked to a friend about my background\"\n",
    "cover_letter(a, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0feb3cbe-686a-4a97-9ca3-a0cb32a24c5d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python (llms)",
   "language": "python",
   "name": "llms"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
